package com.printeverywhere.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.delegator.UserServiceDelegator;
import com.printeverywhere.dto.BlockedBuyer;
import com.printeverywhere.dto.User;
import com.printeverywhere.util.SearchCriteria;
import com.printeverywhere.util.SessionUtil;

public class BlockBuyerAction extends FrontBaseSupport implements ServletRequestAware {

	private String id = null;
	private static String userid = null;
	private String blockbuyer = null;
	private String[] strArr;
	private String str;

	private HttpServletRequest request;

	private static final long serialVersionUID = 1L;

	public BlockBuyerAction(){
		super(true, false);
		setMyAccount(true);
	}

	@SuppressWarnings("unchecked")
	public String execute() throws Exception {
		SearchCriteria searchCriteria = new SearchCriteria();
		searchCriteria.setAttribute("userByUserId.id", SessionUtil.getUser(this.request).getId());
		List<BlockedBuyer> blockBuyerList = (List<BlockedBuyer>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, BlockedBuyer.class);
		str = "";
		for (BlockedBuyer blockedBuyer : blockBuyerList) {
			str += blockedBuyer.getUserByBlockedUserId().getUserId() + ",";
		}

		if(blockBuyerList.size() > 0)
			str = str.substring(0, str.length() - 1);

		return SUCCESS;
	}

	public String blockUser() throws Exception {
		User user = SessionUtil.getUser(request);
		CommonFunctionDelegator.executeQuery("DELETE FROM BlockedBuyer blockedBuyer WHERE userByUserId.id = " + user.getId());


		if (str != null)
			strArr = str.split(",");
		for (int i = 0; i < strArr.length; i++) {
			BlockedBuyer block = new BlockedBuyer();
			User blockUser = UserServiceDelegator.getUserByUserId(strArr[i].trim());

			if(blockUser != null) {
				block.setUserByBlockedUserId(blockUser);
				block.setUserByUserId(user);
				CommonFunctionDelegator.generalInsertbyobj(block);
			}
		}

		return SUCCESS;
	}


	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public static String getUserid() {
		return userid;
	}

	public static void setUserid(String userid) {
		BlockBuyerAction.userid = userid;
	}

	public String getBlockbuyer() {
		return blockbuyer;
	}

	public void setBlockbuyer(String blockbuyer) {
		this.blockbuyer = blockbuyer;
	}

//	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}

}